package zhao.tx.eecs.Tools;

import android.content.Context;
import android.os.Handler;
import android.util.Log;

import com.andware.volley.AuthFailureError;
import com.andware.volley.Request;
import com.andware.volley.RequestQueue;
import com.andware.volley.Response;
import com.andware.volley.Response.ErrorListener;
import com.andware.volley.RetryPolicy;
import com.andware.volley.VolleyError;
import com.andware.volley.toolbox.GsonRequest;
import com.andware.volley.toolbox.SubStringRequest;

import java.util.List;

/**
 * Created by ishishuji on 14-8-19.
 */
public class VolleyBase {

    public static RequestQueue baseQueue;

    public static final int START = 2;

    public static final int SUCCESS = 1;

    public static final int FAILD = 0;

    public static void setBaseQueue ( RequestQueue requestQueue ) {
        baseQueue = requestQueue;
    }

    public static void cancelAll ( Context context ) {
        if ( baseQueue != null ) {
            baseQueue.cancelAll ( context );
        }
    }

    public static void cancelByTag ( Object tag ) {
        if ( baseQueue != null ) {
            baseQueue.cancelAll ( tag );
        }
    }


    public static <T> void  cancelByAllTag ( List<T> tags ) {
        if ( baseQueue != null ) {
            for ( Object tag : tags ) {
                baseQueue.cancelAll ( tag );
            }
        }
    }

    public static void resumeRequest ( Request request ) {
        if ( baseQueue != null ) {
            baseQueue.add ( request );
        }
    }

    public static void cancelByRequestFilter ( RequestQueue.RequestFilter filter ) {
        if ( baseQueue != null ) {
            baseQueue.cancelAll ( filter );
        }
    }

    public static Request volleyGetBase ( String url , final Handler handler ) {
        // prepare the Request
        if ( baseQueue != null ) {
            SubStringRequest getRequest = new SubStringRequest ( url,new Response.Listener<String> () {
                @Override
                public void onResponse ( String response ) {
                    handler.obtainMessage ( SUCCESS,response ).sendToTarget ();
                }
            } , new ErrorListener () {
                @Override
                public void onErrorResponse ( VolleyError error ) {
                    handler.obtainMessage ( FAILD , error ).sendToTarget ();
                }
            });
            baseQueue.add ( getRequest );
            handler.obtainMessage ( START ).sendToTarget ();
            return  getRequest;
        }
        return null;
    }

    public static <T> Request volleyPostBase ( String url , T requestValue , final Handler handler ) {
        if ( baseQueue != null ) {
            SubStringRequest postRequest = new SubStringRequest ( Request.Method.POST ,url, requestValue ,new Response.Listener<String> () {
                @Override
                public void onResponse ( String response ) {
                    handler.obtainMessage ( SUCCESS,response ).sendToTarget ();
                }
            } , new ErrorListener () {
                @Override
                public void onErrorResponse ( VolleyError error ) {
                    handler.obtainMessage ( FAILD , error ).sendToTarget ();
                }
            });
            baseQueue.add ( postRequest );
            handler.obtainMessage ( START ).sendToTarget ();
            return postRequest;
        }
        return null;
    }




    public static <T> Request volleyGet ( String url , Class<T> cls , final Handler handler ) {
        // prepare the Request
        if ( baseQueue != null ) {
            GsonRequest getRequest = new GsonRequest ( url,cls,new Response.Listener<T> () {
                @Override
                public void onResponse ( T response ) {
                    handler.obtainMessage ( SUCCESS , response ).sendToTarget ();
                }
            } , new ErrorListener () {
                @Override
                public void onErrorResponse ( VolleyError error ) {
                    handler.obtainMessage ( FAILD , error ).sendToTarget ();
                }
            });

            baseQueue.add ( getRequest );
            handler.obtainMessage ( START ).sendToTarget ();
            return getRequest;
        }
        return null;
    }


    public static <T> Request volleyGet ( String url , Class<T> cls , final int location , final Handler handler ) {
        // prepare the Request
        if ( baseQueue != null ) {
            GsonRequest getRequest = new GsonRequest ( url,cls,new Response.Listener<T> () {
                @Override
                public void onResponse ( T response ) {
                    handler.obtainMessage ( SUCCESS , location , 0 , response ).sendToTarget ();

                }
            } , new ErrorListener () {
                @Override
                public void onErrorResponse ( VolleyError error ) {
                    handler.obtainMessage ( FAILD ,  location , 0 , error ).sendToTarget ();
                }
            });

            baseQueue.add ( getRequest );
            handler.obtainMessage ( START ).sendToTarget ();
            return getRequest;
        }
        return null;
    }


    public static <T> Request  volleySameClsPost ( String url , T requestValue , Class<T> cls , final Handler handler ) {
        if ( baseQueue != null ) {
            GsonRequest getRequest = new GsonRequest ( Request.Method.POST ,url, requestValue , cls ,new Response.Listener<T> () {
                @Override
                public void onResponse ( T response ) {
                    handler.obtainMessage ( SUCCESS , response ).sendToTarget ();
                }
            } , new ErrorListener () {
                @Override
                public void onErrorResponse ( VolleyError error ) {
                    handler.obtainMessage ( FAILD , error ).sendToTarget ();
                }
            });
            baseQueue.add ( getRequest );
            handler.obtainMessage ( START ).sendToTarget ();
            return getRequest;
        }
        return null;
    }

    public static <T> Request  volleySameClsPost ( String url , T requestValue , Class<T> cls , final int location , final Handler handler ) {
        if ( baseQueue != null ) {
            GsonRequest getRequest = new GsonRequest ( Request.Method.POST ,url, requestValue , cls ,new Response.Listener<T> () {
                @Override
                public void onResponse ( T response ) {
                    handler.obtainMessage ( SUCCESS ,location ,0,response ).sendToTarget ();
                }
            } , new ErrorListener () {
                @Override
                public void onErrorResponse ( VolleyError error ) {
                    handler.obtainMessage ( FAILD , location, 0 ,  error ).sendToTarget ();
                }
            });
            baseQueue.add ( getRequest );
            handler.obtainMessage ( START ).sendToTarget ();
            return getRequest;
        }
        return null;
    }

    public static <T,U> Request  volleyPost ( String url , T requestValue , Class<U> cls , final Handler handler ) {
        if ( baseQueue != null ) {
            Log.i ( "Post", "postStart" );
            GsonRequest getRequest = new GsonRequest ( Request.Method.POST ,url, requestValue , cls ,new Response.Listener<U> () {
                @Override
                public void onResponse ( U response ) {

                    handler.obtainMessage ( SUCCESS , response ).sendToTarget ();
                }
            } , new ErrorListener () {
                @Override
                public void onErrorResponse ( VolleyError error ) {
                    handler.obtainMessage ( FAILD , error ).sendToTarget ();
                }
            });
            baseQueue.add ( getRequest );

            try {
                Log.i ( "cookie","cookie:"+getRequest.getHeaders ().get ( "Cookie" ) );
                Log.i ("type","type:"+getRequest.getHeaders ().get ( "Content-Type" ));
            } catch ( AuthFailureError authFailureError ) {
                authFailureError.printStackTrace ();
            }

            handler.obtainMessage ( START ).sendToTarget ();
            return getRequest;
        }
        return null;
    }


    public static <T> Request  volleyPutBase ( String url , T requestValue , final Handler handler ) {
        if ( baseQueue != null ) {
            SubStringRequest putRequest = new SubStringRequest ( Request.Method.PUT ,url, requestValue ,new Response.Listener<String> () {
                @Override
                public void onResponse ( String response ) {
                    handler.obtainMessage ( SUCCESS , response ).sendToTarget ();
                }
            } , new ErrorListener () {
                @Override
                public void onErrorResponse ( VolleyError error ) {
                    handler.obtainMessage ( FAILD , error ).sendToTarget ();
                }
            });
            baseQueue.add ( putRequest );
            handler.obtainMessage ( START ).sendToTarget ();
            return putRequest;
        }
        return null;
    }


    public static <T> Request  volleySameClsPut ( String url , T requestValue , Class<T> cls , final Handler handler ) {
        if ( baseQueue != null ) {
            GsonRequest putRequest = new GsonRequest ( Request.Method.PUT ,url, requestValue , cls ,new Response.Listener<T> () {
                @Override
                public void onResponse ( T response ) {
                    handler.obtainMessage ( SUCCESS , response ).sendToTarget ();
                }
            } , new ErrorListener () {
                @Override
                public void onErrorResponse ( VolleyError error ) {

                    handler.obtainMessage ( FAILD , error ).sendToTarget ();
                }
            });
            baseQueue.add ( putRequest );
            handler.obtainMessage ( START ).sendToTarget ();
            return putRequest;
        }
        return null;
    }

    public static <T,U> Request  volleyPut ( String url , T requestValue , Class<U> cls , final Handler handler ) {
        if ( baseQueue != null ) {
            GsonRequest getRequest = new GsonRequest ( Request.Method.PUT ,url, requestValue , cls ,new Response.Listener<U> () {
                @Override
                public void onResponse ( U response ) {
                    handler.obtainMessage ( SUCCESS , response ).sendToTarget ();
                }
            } , new ErrorListener () {
                @Override
                public void onErrorResponse ( VolleyError error ) {
                    handler.obtainMessage ( FAILD , error ).sendToTarget ();
                }
            });
            baseQueue.add ( getRequest );
            handler.obtainMessage ( START ).sendToTarget ();
            return getRequest;
        }
        return null;
    }

    public static <T> Request volleyPostNullBase ( String url , Class<T> cls, final Handler handler ) {
        if ( baseQueue != null ) {
            GsonRequest postRequest = new GsonRequest ( Request.Method.POST, url,cls ,new Response.Listener<T> () {
                @Override
                public void onResponse ( T response ) {
                    handler.obtainMessage ( SUCCESS , response ).sendToTarget ();
                }
            } , new ErrorListener () {
                @Override
                public void onErrorResponse ( VolleyError error ) {
                    handler.obtainMessage ( FAILD , error ).sendToTarget ();
                }
            });
            baseQueue.add ( postRequest );
            handler.obtainMessage ( START ).sendToTarget ();
            return postRequest;
        }
        return null;
    }

}
